package com.example.controller;

import com.example.dao.ManagementUserDAO;
import com.example.model.ManagementUser;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/managementUsers")
public class ManagementUserServlet extends HttpServlet {
    private ManagementUserDAO managementUserDAO = new ManagementUserDAO();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String action = request.getParameter("action");
        if (action == null) {
            action = "";
        }

        switch (action) {
            case "add":
                addManagementUser(request, response);
                break;
            case "edit":
                editManagementUser(request, response);
                break;
            case "update":
                updateManagementUser(request, response);
                break;
            case "delete":
                deleteManagementUser(request, response);
                break;
            case "search":
                ManagementUsers(request, response);
                break;
            default:
                listManagementUsers(request, response);
                break;
        }
    }

    private void listManagementUsers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<ManagementUser> users = managementUserDAO.findAllManagementUsers();
        request.setAttribute("users", users);
        request.getRequestDispatcher("jsp/managementUser/list.jsp").forward(request, response);
    }

    private void addManagementUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        int departmentId = Integer.parseInt(request.getParameter("departmentId"));
        String contactInfo = request.getParameter("contactInfo");

        ManagementUser user = new ManagementUser();
        user.setUsername(username);
        user.setPassword(password);
        user.setName(name);
        user.setDepartmentId(departmentId);
        user.setContactInfo(contactInfo);

        boolean success = managementUserDAO.addManagementUser(user);
        if (success) {
            response.sendRedirect("managementUsers");
        } else {
            request.setAttribute("error", "添加失败");
            request.getRequestDispatcher("jsp/managementUser/add.jsp").forward(request, response);
        }
    }

    private void editManagementUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        ManagementUser user = managementUserDAO.findManagementUserById(id);
        if (user != null) {
            request.setAttribute("user", user);
            request.getRequestDispatcher("jsp/managementUser/edit.jsp").forward(request, response);
        } else {
            request.setAttribute("error", "用户不存在");
            request.getRequestDispatcher("jsp/managementUser/list.jsp").forward(request, response);
        }
    }

    private void updateManagementUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        int departmentId = Integer.parseInt(request.getParameter("departmentId"));
        String contactInfo = request.getParameter("contactInfo");

        ManagementUser user = new ManagementUser();
        user.setManagementId(id);
        user.setUsername(username);
        user.setPassword(password);
        user.setName(name);
        user.setDepartmentId(departmentId);
        user.setContactInfo(contactInfo);

        boolean success = managementUserDAO.updateManagementUser(user);
        if (success) {
            response.sendRedirect("managementUsers");
        } else {
            request.setAttribute("error", "更新失败");
            request.getRequestDispatcher("jsp/managementUser/edit.jsp").forward(request, response);
        }
    }

    private void deleteManagementUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        boolean success = managementUserDAO.deleteManagementUser(id);
        if (success) {
            response.sendRedirect("managementUsers");
        } else {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "删除失败");
        }
    }

    private void ManagementUsers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String keyword = request.getParameter("keyword");
        List<ManagementUser> users = managementUserDAO.searchManagementUsers(keyword);
        request.setAttribute("users", users);
        request.setAttribute("keyword", keyword);
        request.getRequestDispatcher("jsp/managementUser/list.jsp").forward(request, response);
    }
}